﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class SysManage_MenuConfig : UserPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Module_Bind(MENU_TYPE);
            SetPager();
        }
    }

    private void Module_Bind(DropDownList dd)
    {
        string sqlStr = "select data_code,data_name from U_PUB_DATA_T WHERE data_type = 'SysModule'";
        dd.DataSource = OracleHelper.ExecuteDataSet(sqlStr, CommandType.Text, null);
        dd.DataTextField = "data_name";
        dd.DataValueField = "data_code";
        dd.DataBind();
    }

    private void SetPager()
    {
        PagedDataSource pds = new PagedDataSource();
        DataSet ds = GetMenu();
        MenuPager.RecordCount = ds.Tables[0].Rows.Count;
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = MenuPager.PageSize;
        pds.CurrentPageIndex = MenuPager.CurrentPageIndex - 1;
        MenuList.DataSource = pds;
        MenuList.DataBind();
    }

    private DataSet GetMenu()
    {
        string sqlStr = @"select T2.DATA_NAME AS Menu_Type,T1.MENU_TITLE,T1.MENU_URL,T1.MENU_NUMBER
                                   ,T1.MENU_STATUS,T1.CREATE_DATE,CASE T1.IF_MOBILE WHEN 'Y' THEN '是' ELSE '否' END AS IF_MOBILE from U_MENU_T T1 
                              LEFT JOIN U_PUB_DATA_T T2 
                                ON T2.DATA_TYPE = 'SysModule'
                               AND T1.MENU_TYPE = T2.DATA_CODE";
        if (Menu_Title_I.Text.Trim().Length > 0)
        {
            sqlStr = sqlStr + " WHERE T1.MENU_TITLE LIKE '%" + Menu_Title_I.Text.Trim()+"%' ";
        }
        sqlStr += " ORDER BY T2.DATA_NAME,T1.MENU_NUMBER ";
        return OracleHelper.ExecuteDataSet(sqlStr, CommandType.Text, null);
    }

    protected void QueryMenu_Click(object sender, EventArgs e)
    {
        SetPager();
    }

    protected void MenuPager_PageChanged(object sender, EventArgs e)
    {
        SetPager();
    }

    protected void BtnCreate_Click(object sender, EventArgs e)
    {
        if (IFOnlyMenu())
        {
            AddMenu();
            MENU_TYPE.SelectedIndex = -1;
            IF_MOBILE.SelectedIndex = -1;
            MENU_TITLE.Text = "";
            MENU_URL.Text = "";
            Menu_Number.Text = "";
            SetPager();
            JQueryAlert("菜单已新增成功!");
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "fail"
                , "$(function() {$('#AddMenuDiv').dialog('open');alert(菜单名称已存在，请重新输入。');})", true);
        }
    }

    private bool IFOnlyMenu()
    {
        string sqlStr = "select COUNT(1) AS c from U_MENU_T WHERE Menu_Title = :Menu_Title";
        OracleParameter[] _params = {
            new OracleParameter(":Menu_Title",OracleType.NVarChar,8)
        };
        _params[0].Value = MENU_TITLE.Text.Trim();
        return OracleHelper.ExecuteScalar(sqlStr, CommandType.Text, _params).ToString().Equals("0");
    }

    private int AddMenu()
    {
        string sqlStr = @"INSERT INTO U_MENU_T(MENU_TYPE, MENU_TITLE, MENU_URL, MENU_STATUS, Menu_Number,CREATER, CREATE_DATE,IF_MOBILE)
                                    VALUES(:MENU_TYPE, :MENU_TITLE, :MENU_URL, '是', :Menu_Number,:CREATER, SYSDATE,:IF_MOBILE)";
        OracleParameter[] _params = {
            new OracleParameter(":MENU_TYPE",OracleType.VarChar,20),
            new OracleParameter(":MENU_TITLE",OracleType.NVarChar,8),
            new OracleParameter(":MENU_URL",OracleType.VarChar,64),
            new OracleParameter(":CREATER",OracleType.VarChar,16),
            new OracleParameter(":Menu_Number",OracleType.Int16),
            new OracleParameter(":IF_MOBILE",OracleType.VarChar,2)
        };
        _params[0].Value = MENU_TYPE.SelectedValue;
        _params[1].Value = MENU_TITLE.Text.Trim();
        _params[2].Value = MENU_URL.Text.Trim();
        _params[3].Value = LoginUser;
        _params[4].Value = Int16.Parse(Menu_Number.Text.Trim());
        _params[5].Value = IF_MOBILE.SelectedValue;
        return OracleHelper.ExecuteNonQuery(sqlStr, CommandType.Text, _params);
    }

    protected void MenuList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        MenuList.EditIndex = -1;
        SetPager();
    }

    protected void MenuList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        MenuList.EditIndex = e.NewEditIndex;
        SetPager();
    }

    protected void MenuList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string sqlStr = @"UPDATE U_MENU_T SET MENU_TYPE=:MENU_TYPE,MENU_URL=:MENU_URL,MENU_STATUS=:MENU_STATUS
                                       ,MENU_NUMBER=:MENU_NUMBER,MODIFIER=:MODIFIER,MODIFY_DATE=SYSDATE,IF_MOBILE=:IF_MOBILE 
                                 WHERE MENU_TITLE=:MENU_TITLE";
        DropDownList dd = (DropDownList)MenuList.Rows[e.RowIndex].FindControl("MENU_TYPE_I");
        TextBox url = MenuList.Rows[e.RowIndex].FindControl("MENU_URL_I") as TextBox;
        TextBox number = MenuList.Rows[e.RowIndex].FindControl("Menu_Number_I") as TextBox;
        DropDownList dd2 = (DropDownList)MenuList.Rows[e.RowIndex].FindControl("MENU_STATUS_I");
        DropDownList dd3 = (DropDownList)MenuList.Rows[e.RowIndex].FindControl("IF_MOBILE_I");
        OracleParameter[] _params = {
            new OracleParameter(":MENU_TYPE",OracleType.VarChar,20),
            new OracleParameter(":MENU_URL",OracleType.VarChar,64),
            new OracleParameter(":MENU_STATUS",OracleType.VarChar,4),
            new OracleParameter(":MENU_NUMBER",OracleType.Int32),
            new OracleParameter(":MODIFIER",OracleType.VarChar,16),
            new OracleParameter(":MENU_TITLE",OracleType.NVarChar,8),
            new OracleParameter(":IF_MOBILE",OracleType.NVarChar,2)
        };
        _params[0].Value = dd.SelectedValue;
        _params[1].Value = url.Text.Trim();
        _params[2].Value = dd2.SelectedValue;
        _params[3].Value = number.Text.Trim();
        _params[4].Value = LoginUser;
        _params[5].Value = this.MenuList.DataKeys[e.RowIndex]["MENU_TITLE"].ToString();
        _params[6].Value = dd3.SelectedValue;
        int res = OracleHelper.ExecuteNonQuery(sqlStr, CommandType.Text, _params);
        if (res > 0)
        {
            MenuList.EditIndex = -1;
            SetPager();
            JQueryAlert("菜单数据更新成功!");
        }
    }

    protected void MenuList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate| DataControlRowState.Edit))
            {
                string MENUTITLE = this.MenuList.DataKeys[e.Row.RowIndex]["MENU_TITLE"].ToString();
                string sqlStr = "select menu_type,MENU_STATUS,IF_MOBILE from U_MENU_T where MENU_TITLE = '" + MENUTITLE + "'";
                DataRow dr = OracleHelper.ExecuteDataSet(sqlStr, CommandType.Text, null).Tables[0].Rows[0];
                DropDownList dd = (DropDownList)e.Row.Cells[1].FindControl("MENU_TYPE_I");
                if (dd != null)
                {
                    Module_Bind(dd);
                    dd.SelectedValue = dr["menu_type"].ToString();
                }
                DropDownList dd2 = (DropDownList)e.Row.Cells[1].FindControl("MENU_STATUS_I");
                if (dd2 != null)
                {
                    dd2.SelectedValue = dr["MENU_STATUS"].ToString();
                }
                DropDownList dd3 = (DropDownList)e.Row.Cells[1].FindControl("IF_MOBILE_I");
                if (dd3 != null)
                {
                    dd3.SelectedValue = dr["IF_MOBILE"].ToString();
                }
            }
        }
    }
}
